{#
    LimeSurvey
    Copyright (C) 2007-2017 The LimeSurvey Project Team / Louis Gac
    All rights reserved.
    License: GNU/GPL License v2 or later, see LICENSE.php
    LimeSurvey is free software. This version may have been modified pursuant
    to the GNU General Public License, and as distributed it includes or
    is derivative of works licensed under the GNU General Public License or
    other free or open source software licenses.
    See COPYRIGHT.php for copyright notices and details.

    (¯`·._.·(¯`·._.· Google Analytics  ·._.·´¯)·._.·´¯)

    This file generates the scripts for Google Analytics, as set by user in survey settings.
    See: survey settings
#}
{% if aSurveyInfo.googleanalyticsapikey  is defined and aSurveyInfo.googleanalyticsapikey and aSurveyInfo.googleanalyticsstyle %}
    {# Check if the tracking ID is a Universal Analytics (legacy) or a Google Analytics 4 ID #}
    {% if aSurveyInfo.googleanalyticsapikey starts with 'UA-' %}
        {# Legacy UA #}
        {% if aSurveyInfo.googleanalyticsstyle  is defined and aSurveyInfo.googleanalyticsstyle == 1 %}
            <script>
            (function(i,s,o,g,r,a,m){ i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                (i[r].q=i[r].q||[]).push(arguments) },i[r].l=1*new Date();a=s.createElement(o),
                m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
            })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

            ga('create', '{{aSurveyInfo.googleanalyticsapikey}}', 'auto');  // Replace with your property ID.
            ga('send', 'pageview');
            </script>
        {% else %}
            {% set trackUrl = getGoogleAnalyticsTrackingUrl(aSurveyInfo.sid, aSurveyInfo.trackUrlPageName ?? '') %}
            <script>
            (function(i,s,o,g,r,a,m){ i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments) }
            ,i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
            })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

            ga('create', '{{ aSurveyInfo.googleanalyticsapikey }}', 'auto');
            ga('send', 'pageview');
            ga('send', 'pageview', '{{ trackUrl|escape }}');
            </script>
        {% endif %}
    {% else %}
        {# GA4 #}
        <!-- Google tag (gtag.js) -->
        <script async src="https://www.googletagmanager.com/gtag/js?id={{aSurveyInfo.googleanalyticsapikey}}"></script>
        <script>
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}
            gtag('js', new Date());

            gtag('config', '{{aSurveyInfo.googleanalyticsapikey}}');

            {% if not (aSurveyInfo.googleanalyticsstyle is defined and aSurveyInfo.googleanalyticsstyle == 1) %}
                {% set trackUrl = getGoogleAnalyticsTrackingUrl(aSurveyInfo.sid, aSurveyInfo.trackUrlPageName ?? '') %}
                gtag('event', 'page_view', { page_title: '{{ trackUrl|escape }}' })
            {% endif %}
        </script>
    {% endif %}
{% endif %}
